package com.ymatou.iapi.livebuyer.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.SetNotSpeekingBean;
import com.ymatou.iapi.livebuyer.service.SetNotSpeekingCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.SpeakingNotAllowed;
/**
*设置云通讯全局禁言
*FreeMaker Template自动生成代码
*/
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
@TestSuite
public class Ts_SetNotSpeeking{
	private static SetNotSpeekingBean setnotspeekingBean;
	private static SetNotSpeekingCall setnotspeekingCall;
	LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置云通讯全局禁言");
	}

	@Before
	public void caseUp() {
		setnotspeekingBean=new SetNotSpeekingBean();
		setnotspeekingCall=new SetNotSpeekingCall();
		LiveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setnotspeekingCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private Integer getId(Integer userId) {
//		LiveRoomWapper.selectSpeakingNotAllowed(userId).stream().filter(sp->(sp.getState().));
		Integer id=0;
		try {
		 id=LiveRoomWapper.selectSpeakingNotAllowed(userId,0).stream().filter(sp->(sp.getState()==0&&sp.getEndTime().after(YMTDateUtil.getDate()))).findFirst().get().getId();
		}catch (Exception e) {
		}
		return id;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNotSpeeking_001() {
		Logger.start(true,"设置云通讯全局禁言成功");
		try {
			Integer userId=1;
			LiveRoomCallService.DisableNotSpeaking(userId);
			Integer id=getId(userId);
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(1000);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			SpeakingNotAllowed SpeakingNotAllowed = LiveRoomWapper.selectSpeakingNotAllowed(userId,0).get(0);
			Logger.verifyEquals(setnotspeekingBean.getGroupNoSpeakingSecond()*1000l, SpeakingNotAllowed.getEndTime().getTime()-SpeakingNotAllowed.getBeginTime().getTime(), "验证时长");
			Logger.verifyEquals(setnotspeekingBean.getOperator(), SpeakingNotAllowed.getOperator(), "验证Operator");
			Logger.verifyEquals(setnotspeekingBean.getReason(), SpeakingNotAllowed.getReason(), "验证Reason");
			Logger.verifyEquals(0, SpeakingNotAllowed.getState(), "验证Reason");
			//TODO  http://livebuyer.iapi.ymatou.com/api/GetNotSpeeking  {"account":"68528"} 可以手工验证 链接第三方站点腾讯云
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetNotSpeeking_002() {
		Logger.start(true,"不输入userid失败");
		try {
//			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(100);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("100", setnotspeekingCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetNotSpeeking_003() {
		Logger.start(true,"不输入groupNoSpeakingSecond默认无限时间");
		try {
			Integer userId=1;
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
//			setnotspeekingBean.setGroupNoSpeakingSecond(100);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			SpeakingNotAllowed SpeakingNotAllowed = LiveRoomWapper.selectSpeakingNotAllowed(userId,0).get(0);
//			Logger.verifyEquals(setnotspeekingBean.getGroupNoSpeakingSecond()*1000l, SpeakingNotAllowed.getEndTime().getTime()-SpeakingNotAllowed.getBeginTime().getTime(), "验证时长");
			Logger.verifyEquals(YMTDateUtil.getDate("9999-09-09 00:00:00", YMTDateUtil.YMDHMS), SpeakingNotAllowed.getEndTime(), "验证EndTime");
			Logger.verifyEquals(setnotspeekingBean.getOperator(), SpeakingNotAllowed.getOperator(), "验证Operator");
			Logger.verifyEquals(setnotspeekingBean.getReason(), SpeakingNotAllowed.getReason(), "验证Reason");
			Logger.verifyEquals(0, SpeakingNotAllowed.getState(), "验证State");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetNotSpeeking_004() {
		Logger.start(true,"不输入operator失败");
		try {
			Integer userId=TestDataManager.getRandomIntNum(5);
			Integer id=getId(userId);
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(100);
			setnotspeekingBean.setReason("reason");
//			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("100", setnotspeekingCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetNotSpeeking_005() {
		Logger.start(true,"不输入reason失败");
		try {
			Integer userId=TestDataManager.getRandomIntNum(5);
			Integer id=getId(userId);
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(100);
//			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("100", setnotspeekingCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetNotSpeeking_006() {
		Logger.start(true,"groupNoSpeakingSecond为0永久禁言");
		try {
			Integer userId=1;
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(0);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			SpeakingNotAllowed SpeakingNotAllowed = LiveRoomWapper.selectSpeakingNotAllowed(userId,0).get(0);
			Logger.debug(FormatUtil.beanToGSONString( SpeakingNotAllowed));
//			Logger.verifyEquals(setnotspeekingBean.getGroupNoSpeakingSecond()*1000l, SpeakingNotAllowed.getEndTime().getTime()-SpeakingNotAllowed.getBeginTime().getTime(), "验证时长");
			Logger.verifyEquals(YMTDateUtil.getDate("9999-09-09 00:00:00", YMTDateUtil.YMDHMS), SpeakingNotAllowed.getEndTime(), "验证EndTime");
			Logger.verifyEquals(setnotspeekingBean.getOperator(), SpeakingNotAllowed.getOperator(), "验证Operator");
			Logger.verifyEquals(setnotspeekingBean.getReason(), SpeakingNotAllowed.getReason(), "验证Reason");
			Logger.verifyEquals(0, SpeakingNotAllowed.getState(), "验证State");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetNotSpeeking_007() {
		Logger.start(true,"groupNoSpeakingSecond过期后解除禁言");
		try {
			Integer userId=1;
			Integer id=getId(userId);
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(2);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			SpeakingNotAllowed SpeakingNotAllowed = LiveRoomWapper.selectSpeakingNotAllowed(userId,0).get(0);
			Logger.verifyEquals(setnotspeekingBean.getGroupNoSpeakingSecond()*1000l, SpeakingNotAllowed.getEndTime().getTime()-SpeakingNotAllowed.getBeginTime().getTime(), "验证时长");
			Logger.verifyEquals(setnotspeekingBean.getOperator(), SpeakingNotAllowed.getOperator(), "验证Operator");
			Logger.verifyEquals(setnotspeekingBean.getReason(), SpeakingNotAllowed.getReason(), "验证Reason");
			Logger.verifyEquals(0, SpeakingNotAllowed.getState(), "验证State");
			Thread.sleep(2000);
			//TODO 尝试
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNotSpeeking_008() {
		Logger.start(true,"重复禁言失败");
		try {
			Integer userId=1;
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(100);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();	
			Thread.sleep(1000);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("400", setnotspeekingCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNotSpeeking_009() {
		Logger.start(true,"设置云通讯全局禁言成功操作人20字符");
		try {
			Integer userId=1;
			Integer id=getId(userId);
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(1000);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("12345678901234567890");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			SpeakingNotAllowed SpeakingNotAllowed = LiveRoomWapper.selectSpeakingNotAllowed(userId,0).get(0);
			Logger.verifyEquals(setnotspeekingBean.getGroupNoSpeakingSecond()*1000l, SpeakingNotAllowed.getEndTime().getTime()-SpeakingNotAllowed.getBeginTime().getTime(), "验证时长");
			Logger.verifyEquals(setnotspeekingBean.getOperator(), SpeakingNotAllowed.getOperator(), "验证Operator");
			Logger.verifyEquals(setnotspeekingBean.getReason(), SpeakingNotAllowed.getReason(), "验证Reason");
			Logger.verifyEquals(0, SpeakingNotAllowed.getState(), "验证Reason");
			//TODO  http://livebuyer.iapi.ymatou.com/api/GetNotSpeeking  {"account":"68528"} 可以手工验证 链接第三方站点腾讯云
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNotSpeeking_010() {
		Logger.start(true,"操作人超过20字符设置云通讯全局禁言失败");
		try {
			Integer userId=1;
			Integer id=getId(userId);
			LiveRoomCallService.DisableNotSpeaking(userId);
			setnotspeekingBean.setUserId(userId);
			setnotspeekingBean.setGroupNoSpeakingSecond(1000);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("123456789012345678901");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("100", setnotspeekingCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetNotSpeeking_011() {
		Logger.start(true,"设置云通讯直播间禁言成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			initcreatebean.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("SU"+TestDataManager.getRandomNum(5),initcreatebean.getLiveDto().getUserId()));
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			int liveid = livedto.getId();
			//需要tx云存在的用户
			Integer uid = 24636166;
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			LiveRoomCallService.enterGroup(liveid, uid);
			LiveRoomCallService.DisableNotSpeaking(uid);
			Integer id=getId(uid);
			LiveRoomCallService.DisableNotSpeaking(uid);
			setnotspeekingBean.setUserId(uid);
			setnotspeekingBean.setLiveId(liveid);
			setnotspeekingBean.setGroupNoSpeakingSecond(1000);
			setnotspeekingBean.setReason("reason");
			setnotspeekingBean.setOperator("autotest");
			setnotspeekingCall.setData(setnotspeekingBean);
			setnotspeekingCall.callService();			
			Logger.verifyEquals("200", setnotspeekingCall.getString("code"), "验证code");
			SpeakingNotAllowed SpeakingNotAllowed = LiveRoomWapper.selectSpeakingNotAllowed(uid,0).get(0);
			Logger.verifyEquals(setnotspeekingBean.getGroupNoSpeakingSecond()*1000l, SpeakingNotAllowed.getEndTime().getTime()-SpeakingNotAllowed.getBeginTime().getTime(), "验证时长");
			Logger.verifyEquals(setnotspeekingBean.getOperator(), SpeakingNotAllowed.getOperator(), "验证Operator");
			Logger.verifyEquals(setnotspeekingBean.getReason(), SpeakingNotAllowed.getReason(), "验证Reason");
			Logger.verifyEquals(0, SpeakingNotAllowed.getState(), "验证Reason");
			Logger.verifyEquals(liveid, SpeakingNotAllowed.getLiveId(), "验证liveid");
			//TODO  http://livebuyer.iapi.ymatou.com/api/GetNotSpeeking  {"account":"68528"} 可以手工验证 链接第三方站点腾讯云
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}